using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SigmaStock_Components;
using System.Data.OleDb;

namespace project
{
    public partial class Publisher : Form
    {
        OleDbConnection conn;
        public static String distname = "";
        string query = "";

        public Publisher()
        {
            InitializeComponent();
            textBox1.ReadOnly = false;
            conn = DBComponent.Mag_DealerDB.dbconnection();
            conn.Open();
        }

        public string a
        {
            set
            {
                distname = value;
            }
        }

        private void UpdateDataGrid()
        {
            String s;
            s = "Select * from publisher";
            OleDbCommand cmd1 = new OleDbCommand(s, conn);
            OleDbDataAdapter da = new OleDbDataAdapter(s, conn);
            DataSet ds = new DataSet();
            da.Fill(ds, "publisher");
            publisherDataGridView.DataSource = ds;
            publisherDataGridView.DataMember = "publisher";
            publisherBindingSource.ResetBindings(false);
            this.Refresh();
        }

        private void publisherBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.publisherBindingSource.EndEdit();
            this.publisherTableAdapter.Update(this.mag_dealerDataSet.publisher);

        }

        private void Publisher_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'mag_dealerDataSet.publisher' table. You can move, or remove it, as needed.
            this.publisherTableAdapter.Fill(this.mag_dealerDataSet.publisher);

        }

        public void UpdatePublisher()
        {
            int flag = 0;
            //MessageBox.Show(distname);
            String query = "select * from publisher where PNAME= '" + distname + "';";

            OleDbCommand command = new OleDbCommand(query, conn);
            OleDbDataReader reader = command.ExecuteReader();

            while (reader.Read())
                flag = 1;
            if (flag == 1)
                MessageBox.Show("Publisher record deleted !!!");
            else
                MessageBox.Show("Publisher record doesn't exist");
            reader.Close();

            String query1 = "delete PNAME from publisher where PNAME='" + distname + "';";

            OleDbCommand cmd = new OleDbCommand(query1, conn);
            cmd.ExecuteNonQuery();
            UpdateDataGrid();
        }


        private void ret_insert_Click(object sender, EventArgs e)
        {
            if (ValidatePublisherData() && InsertValidation())
            {
                String dname = (String)textBox1.Text;
                String dadd1 = (String)richTextBox1.Text;
                String dadd2 = (String)richTextBox2.Text;
                String email = (String)textBox2.Text;
                int contactno = int.Parse(textBox3.Text);
                try
                {
                    string sqlquery = "insert into publisher values('" + dname + "','" + dadd1 + "','" + dadd2 + "','" + email + "','" + contactno + "');";
                    OleDbCommand cmd = new OleDbCommand(sqlquery, conn);
                    int res = cmd.ExecuteNonQuery();
                    MessageBox.Show("Data Inserted Successfully !!!");
                    UpdateDataGrid();
                    Publisher_Credit.pname = dname;
                    Program.InvokeModal(this, "project.Publisher_Credit");
                    //   Console.WriteLine(res);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
            }

        }

        private bool ValidatePublisherData()
        {
            if ((String.IsNullOrEmpty(textBox1.Text)) || !(Validation.IsAlphaNumeric(textBox1.Text)))
            {
                textBox4.Visible = true;
                textBox4.Text = "Check Publisher Name";
                Console.Beep();
                return false;
            }

            if ((String.IsNullOrEmpty(richTextBox1.Text)))
            {

                textBox4.Visible = true;
                textBox4.Text = "Check Address1";
                Console.Beep();
                return false;
            }

            if ((String.IsNullOrEmpty(richTextBox2.Text)))
            {
                textBox4.Text = "Check Address2";
                Console.Beep();
                return false;
            }

            if ((String.IsNullOrEmpty(textBox2.Text)))
            {

                textBox4.Visible = true;
                textBox4.Text = "Check email id";
                Console.Beep();
                return false;
            }

            if ((String.IsNullOrEmpty(textBox3.Text)) || !(Validation.IsNumber(textBox3.Text)))
            {

                textBox4.Visible = true;
                textBox4.Text = "Check Contact no";
                Console.Beep();
                return false;
            }


            return true;
        }

        private bool InsertValidation()
        {
            query = "select PNAME from publisher;";
            List<string> pub_list = new List<string>();
            OleDbCommand command = new OleDbCommand(query, conn);
            OleDbDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                pub_list.Add(reader[0].ToString());
            }

            if (pub_list.Contains(textBox1.Text))
            {

                textBox4.Visible = true;
                MessageBox.Show("Publisher already exists in database !!!");
                textBox1.ResetText();
                return false;
            }
            return true;
        }

        private void ret_delete_Click(object sender, EventArgs e)
        {
            Program.InvokeModalNoClose(this, "project.Form4");
            MessageBox.Show("Data Deleted Successfully !!!");
            UpdatePublisher();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (ValidatePublisherData())
            {
                String dname = (String)textBox1.Text;
                String dadd1 = (String)richTextBox1.Text;
                String dadd2 = (String)richTextBox2.Text;
                String email = (String)textBox2.Text;
                int contactno = int.Parse(textBox3.Text);

                try
                {
                    string sqlquery = "update publisher set PADDRESS1='" + dadd1 + "',PADDRESS2='" + dadd2 + "',PEMAILID='" + email + "',PCONTACTNO='" + contactno + "' where PNAME='" + dname + "';";
                    OleDbCommand cmd = new OleDbCommand(sqlquery, conn);
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Data Modified Successfully !!!");

                    UpdateDataGrid();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
            }
        }



        private void Publisher_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (Application.OpenForms.Count == 1)
            {
                Program.main_form.Show();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.ReadOnly = false;
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.ResetText();
            richTextBox1.ResetText();
            richTextBox2.ResetText();
        }


        private void publisherDataGridView_CellClick_1(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                DataGridViewRowCollection datarows = publisherDataGridView.Rows;
                DataGridViewRow selrow = datarows[e.RowIndex];
                selrow.Selected = true;
                foreach (DataGridViewRow dr in datarows)
                {
                    if (dr.Selected)
                    {
                        textBox1.ReadOnly = true;
                        textBox1.Text = dr.Cells[0].Value.ToString();
                        richTextBox1.Text = dr.Cells[1].Value.ToString();
                        richTextBox2.Text = dr.Cells[2].Value.ToString();
                        textBox2.Text = dr.Cells[3].Value.ToString();
                        textBox3.Text = dr.Cells[4].Value.ToString();
                    }

                }
            }
            catch (Exception ex)
            {

            }
        }
    }
    
}